<template>
    <a-modal
      title="编辑用户密级"
      :visible="visible"
      @ok="() => {this.$emit('ok')}"
      @cancel="() => {this.$emit('cancel')}"
      maskClosable
    >
        <a-form :form="form">
            <a-row>
                <a-col span="24">
                    <a-form-item label="用户账号" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input placeholder="请输入用户账号" v-decorator="['userNameId']" disabled/>
                    </a-form-item>
                </a-col>
                <a-col span="24">
                    <a-form-item label="用户姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input placeholder="请输入用户姓名" v-decorator="['userName']" disabled/>
                    </a-form-item>
                </a-col>
            </a-row>
            <a-row>
                <a-col span="24">
                    <a-form-item label="所属组织" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <depart-popup placeholder="请选择所属组织" v-decorator="['userOrgId']" disabled></depart-popup>
                    </a-form-item>
                </a-col>
            </a-row>
            <a-row>
                <a-col span="24">
                    <a-form-item label="密级" :labelCol="labelCol" :wrapperCol="wrapperCol">
						<a-select
							placeholder="请选择密级"
							v-decorator="['userConfidentiality', { rules: [{ required: true, message: '请选择密级！' }]}]"
						>
							<a-select-option :value="item.secrecyCode" v-for="item in secrecyList" :key="item.uuid">
								{{item.secrecyName}}
							</a-select-option>
						</a-select>
                    </a-form-item>
                </a-col>
            </a-row>
            <a-row>
                <a-col span="24">
                    <a-form-item label="安全级别" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <t-dict-select-tag type="select" dictCode="userSecurityLevel" placeholder="请选择密级" v-decorator="['userSecurityLevel', {
                            initialValue: 'userSecurityLevel.01',
                            rules: [{ required: true, message: '请选择安全级别！' }]
                        }]" />
                    </a-form-item>
                </a-col>
            </a-row>
            <a-form-item class="hiddenItem">
                <a-input v-decorator="['uuid']" disabled type="hidden" />
            </a-form-item>
            <a-form-item class="hiddenItem">
                <a-input v-decorator="['userSex']" disabled type="hidden" />
            </a-form-item>
            <a-form-item class="hiddenItem">
                <a-input v-decorator="['userGroup']" disabled type="hidden" />
            </a-form-item>
            <a-form-item class="hiddenItem">
                <t-dict-select-tag type="select" dictCode="UserInfoStatus" placeholder="请选择账号状态" v-decorator="['userStatus', {initialValue: 'UserInfoStatus.03'}]" />
            </a-form-item>
			<a-form-item class="hiddenItem">
                <t-dict-select-tag type="select" dictCode="UserLock" placeholder="请选择锁定状态" v-decorator="['userLock', {initialValue: 'UserLock.02'}]" />
            </a-form-item>
            <a-form-item class="hiddenItem">
                <a-input v-decorator="['userReportsTo']" disabled type="hidden" />
            </a-form-item>
            <a-form-item class="hiddenItem">
                <a-input v-decorator="['userPosition']" disabled type="hidden" />
            </a-form-item>
        </a-form>
    </a-modal>
</template>

<script>
import pick from "lodash.pick";
import TMultiSelectTag from "@/components/TopVUI/dict/TMultiSelectTag";
import TreeSelect from "@/components/TopVUI/custom/TreeSelect";
import { getFilter } from "@/utils/TopVUI";
import DepartPopup from "@/components/TopVUI/custom/DepartPopup";
import {PersonnelPopup} from "@/components";
import ARow from "ant-design-vue/es/grid/Row";
import { TImageUpload, TDictSelectTag } from "@/components";
// 表单字段
const fields = [
	"uuid",
	"userName",
	"userNameId",
	"nickName",
	"userSex",
	"userAge",
	"userEmail",
	"userOrgId",
	"userReportsTo",
	"userPosition",
	"userGroup",
	"userEntryTime",
	"userStatus",
	"userLock",
	"userSort",
	"userRemark",
	"userHeadPortrait",
	"userBirthday",
	"userPersonalSignature",
	"userFamilyName",
	"userNativePlace",
	"userTelephone",
	"userCellphone",
	"politicalOutlook",
	"userEducation",
	"idCard",
	"idCardNo",
	"depositBank",
	"bankAccounts"
];
export default {
	props: {
		visible: {
			type: Boolean,
			required: true,
		},
		loading: {
			type: Boolean,
			default: () => false,
		},
		model: {
			type: Object,
			default: () => null,
		},
	},
	components: { ARow, TMultiSelectTag, TreeSelect, TImageUpload, DepartPopup, PersonnelPopup, TDictSelectTag },
	data() {
		return {
            title: "",
			labelCol: {
				xs: {
					span: 24,
				},
				sm: {
					span: 4,
				},
			},
			wrapperCol: {
				xs: {
					span: 24,
				},
				sm: {
					span: 20,
				},
			},
			form: this.$form.createForm(this),
			authGroup: [],
			userGroup: "",
			dataAuth: "",
			urlAuth: {
				fathersUrl: "/system/sysRole/getListSysRole",
			},
			positionArr: [],
			positionList: [],
			authGroupArr: [],
			secrecyList: [],
		};
	},
	created() {
		// 获取数据权限
		this.$post(this.urlAuth.fathersUrl).then((res) => {
			if (res.statusCode === 200) {
				this.authGroup = res.data;
			}
		});
		// 防止表单未注册
		fields.forEach((v) => this.form.getFieldDecorator(v));
		// 当 model 发生改变时，为表单设置值
		this.$watch("model", () => {
			if (this.model && JSON.stringify(this.model) != "{}") {
				this.positionArr = this.model?.userPosition.split(",");
				this.authGroupArr = this.model?.userGroup.split(",").map(parseFloat);
			}
			this.$nextTick(() => {
                this.model && this.form.setFieldsValue(pick(this.model, fields));
            })
		});
	},
	watch: {
		visible(val) {
			if (val) {
				this.form.resetFields('userConfidentiality')
				this.$post('/system/sysSecrecy/getListSysSecrecyByLevelId').then(res => {
					if (res.statusCode === 200) this.secrecyList = res.data
				})
			}
		}
	},
	methods: {
		// 新增编辑
		add(e, data) {
			data.forEach(item => {
				if (item.id == e.orgId) {
					e.orgName = item.text
				}
			})
		},
		edit(e, data) {
			this.add(e, data);
		},
		// 过滤
		getFilter(sourceData, children) {
			let arr = [];
			if (sourceData) {
				sourceData.forEach((e, index) => {
					children.forEach((c, index) => {
						if (e.id == c.pid) {
							c.key = c.id;
							c.children = c.state === "closed" ? [] : "";
							if (e.children) e.children.push(c);
						} else {
							c.key = c.id;
						}
					});
					if (e.children) {
						// 子级有数据的时候 循环添加数据
						getFilter(e.children, children);
					}
				});
				arr = sourceData;
			}
			return arr;
		},
	},
};
</script>

<style lang="scss" scoped>

</style>